//
//  IMMessageCell.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/29.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "IMMessageCell.h"

@interface IMMessageCell ()

@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UIView *messageContentView;
@property (nonatomic, strong) UILabel *messageLab;
@property (nonatomic, strong) UIActivityIndicatorView *indicator;
@property (nonatomic, strong) UIButton *reSendBtn;

@end

@implementation IMMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubViews];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setMessage:(IMMessageEntity *)message
{
    _message = message;
        
    self.messageLab.text = message.content;
    if (message.isAnimation) {
        [self startAnimation];
    }
    else {
        [self stopAnimation];
    }
    if (message.isShowReSend) {
        [self showReSend];
    }
    else {
        [self hiddenReSend];
    }
}

- (void)startAnimation
{
    self.indicator.hidden = NO;
    [self.indicator startAnimating];
}

- (void)stopAnimation
{
    self.indicator.hidden = YES;
    [self.indicator stopAnimating];
}

- (void)showReSend
{
    self.reSendBtn.hidden = NO;
}

- (void)hiddenReSend
{
    self.reSendBtn.hidden = YES;
}

- (void)setupSubViews
{
    [self addSubview:self.icon];
    [self addSubview:self.nameLab];
    [self addSubview:self.messageContentView];
    [self.messageContentView addSubview:self.messageLab];
    [self addSubview:self.indicator];
    [self addSubview:self.reSendBtn];
    
    self.messageLab.textColor = [UIColor whiteColor];
    self.messageContentView.backgroundColor = kColorGradientBegin;
    
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-kWidth(15));
        make.top.equalTo(self).offset(kWidth(10));
        make.width.height.equalTo(@(kWidth(40)));
    }];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.icon);
        make.right.equalTo(self.icon.mas_left).offset(-kWidth(6));
    }];
    [self.messageContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.nameLab);
        make.top.equalTo(self.nameLab.mas_bottom).offset(kWidth(6));
        make.left.greaterThanOrEqualTo(self).offset(kWidth(kScreenW/4.0));
        make.bottom.equalTo(self);
    }];
    [self.messageLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.messageContentView).offset(kWidth(10));
        make.bottom.right.equalTo(self.messageContentView).offset(-kWidth(10));
    }];
    [self.indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.messageContentView);
        make.right.equalTo(self.messageContentView.mas_left).offset(-kWidth(6));
    }];
    [self.reSendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.messageContentView);
        make.right.equalTo(self.messageContentView.mas_left).offset(-kWidth(6));
    }];
    [self.reSendBtn addTarget:self action:@selector(reSendAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)reSendAction:(id)sender
{
    self.message.isShowReSend = NO;
    self.message.isAnimation = YES;
    [self hiddenReSend];
    [self startAnimation];
    if (self.reSendHandle) {
        self.reSendHandle(self.message);
    }
}

- (UIImageView *)icon
{
    if (!_icon) {
        _icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"person_headpic"]];
        _icon.layer.cornerRadius = kWidth(20);
        _icon.layer.masksToBounds = YES;
        _icon.backgroundColor = [UIColor whiteColor];
    }
    return _icon;
}

- (UILabel *)nameLab
{
    if (!_nameLab) {
        _nameLab = [[UILabel alloc] init];
        _nameLab.textColor = kColor666666;
        _nameLab.font = [UIFont systemFontOfSize:12];
    }
    return _nameLab;
}

- (UIView *)messageContentView
{
    if (!_messageContentView) {
        _messageContentView = [[UIView alloc] init];
    }
    return _messageContentView;
}

- (UILabel *)messageLab
{
    if (!_messageLab) {
        _messageLab = [[UILabel alloc] init];
        _messageLab.numberOfLines = 0;
        _messageLab.font = [UIFont systemFontOfSize:15];
    }
    return _messageLab;
}

- (UIActivityIndicatorView *)indicator
{
    if (!_indicator) {
        _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _indicator.hidden = YES;
    }
    return _indicator;
}

- (UIButton *)reSendBtn
{
    if (!_reSendBtn) {
        _reSendBtn = [[UIButton alloc] init];
        [_reSendBtn setImage:[UIImage imageNamed:@"reSend"] forState:UIControlStateNormal];
        _reSendBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_reSendBtn setTitleColor:kColor666666 forState:UIControlStateNormal];
        _reSendBtn.hidden = YES;
    }
    return _reSendBtn;
}

@end
